package solution;

import java.util.ArrayList;

import hex.*;
import game.*;

public class HexPlayer extends GamePlayer {
	Path[] paths;
	
	
	public HexPlayer(String n) {
		super(n, new HexState(), false);
	}
	
	
	public void init() {
		paths = new Path[6];
		ArrayList<Point> useless = new ArrayList<Point>();
		
		for (int i = 0; i < paths.length; i++){
			paths[i] = new Path(new Point(0,i), true, 2, useless);
			System.out.println("done with: "+i);
		}
	}
	
	
	public void startGame(String opponent) {}
	
	
	public void timeOfLastMove(double secs) {}
	
	
	public void endGame(int result) { }
	
	
	public void done() { }
	
	
	public GameMove getMove(GameState state, String lastMove) {
		HexState board = (HexState)state;
		HexMove mv = new HexMove();
		int size = HexState.N;
		int maxNumMoves = size*size/2;
		
		mv.row=0;
		mv.col=0;
		
		if (board.moveOK(mv))return mv;
		
		return null;
	}

	public static void main(String [] args) {
		//GamePlayer p = new HexPlayer("SEXY BEAST");  // give your player a name here
		//p.compete(args, 1);
		new HexPlayer("a sexy beast").init();
	}
}

class Point{
	int x,y;
	public Point(int x, int y){
		this.x = x; this.y = y;
	}
	
	public ArrayList<Point> getSurrounding(){
		ArrayList<Point> result = new ArrayList<Point>();
		if (y+1<6)result.add(new Point(x,y+1));
		if (y-1>=0)result.add(new Point(x,y-1));
		if (x+1<6)result.add(new Point(x+1,y));
		if (x-1>=0)result.add(new Point(x-1,y));
		if (x-1>=0 && y+1<6)result.add(new Point(x-1,y+1));
		if (x+1<6 && y-1>=0)result.add(new Point(x+1,y-1));
		return result;
	}
	
	public boolean equals(Point o){
		return o.x==x && o.y==y;
	}
}

class Path{
	static int count = 0;
	ArrayList<Point> allPoints = new ArrayList<Point>();
	public Point myPoint;
	public Path[] subPaths = new Path[6];
	public boolean endPoint = false;
	boolean home;
	
	public Path addPoint(Point p, int i){
		int x = p.x;
		int y = p.y;
		
		if (this.contains(p))return null;
		if (endPoint)return null;
		if (i==0)return null;
		for (Point p2: allPoints)if (p2.equals(p))return null;
		
		int spot = 7;
		if(myPoint.y==y && myPoint.x==x+1)spot = 3-1;
		if (myPoint.y==y && myPoint.x==x-1)spot = 6-1;
		if (myPoint.y==y-1 && myPoint.x==x)spot = 1-1;
		if (myPoint.y==y-1 && myPoint.x==x+1)spot = 2-1;
		if (myPoint.y==y+1 && myPoint.x==x)spot = 4-1;
		if (myPoint.y==y+1 && myPoint.x==x-1)spot = 5-1;
		if (spot!=7){
			subPaths[spot] = new Path(p, home, i-1, allPoints);
			return subPaths[spot];
		}
		return null;
	}

	public boolean contains(Point p){
		if (myPoint.equals(p))return true;
		if (subPaths[0]==null||subPaths[1]==null||subPaths[2]==null||subPaths[3]==null||subPaths[4]==null||subPaths[5]==null)return false;
		for (Path path: subPaths)if (path.contains(p))return true;
		return false;
	}
	
	public Path(Point p, boolean home, int maxMoves, ArrayList<Point> previousPoints){
		this.home = home;
		myPoint = p;
		allPoints.addAll(previousPoints);
		allPoints.add(p);
		
		if (home && p.x==5)endPoint = true;
		if (!home && p.y==5)endPoint = true;
		
		ArrayList<Point> nextOptions = myPoint.getSurrounding();
		System.out.println(count++);
		for (Point n: nextOptions){
			this.addPoint(n, maxMoves);
		}
	}
}

